package org.papervision3d.core.geom
{
    import org.papervision3d.core.*;
    import org.papervision3d.core.geom.*;
    import org.papervision3d.core.proto.*;
    import org.papervision3d.scenes.*;
    
    import flash.utils.Dictionary;
    import org.papervision3d.objects.DisplayObject3D;
    
    // The Vertices3D class lets you create and manipulate groups of vertices.
    public class Vertices3D extends DisplayObject3D
    {
        public var vertices:Array = new Array();

        // Creates a new Points object.
        // The Points GeometryObject3D class lets you create and manipulate groups of vertices.
        // @param    vertices    An array of Vertex3D objects for the vertices of the mesh.
        // @param    initObject  [optional] - An object that contains user defined properties with which to populate the newly created GeometryObject3D.
        // It includes x, y, z, rotationX, rotationY, rotationZ, scaleX, scaleY scaleZ and a user defined extra object.
        // If extra is not an object, it is ignored. All properties of the extra field are copied into the new instance. The properties specified with extra are publicly available.
        public function Vertices3D(name:String = null, initObject:Object = null)
        {
            super(name, initObject);
        }

        // Projects three dimensional coordinates onto a two dimensional plane to simulate the relationship of the camera to subject.
        // This is the first step in the process of representing three dimensional shapes two dimensionally.
        // @param camera Camera.
        public override function project(parent:DisplayObject3D, camera:CameraObject3D, callback:TraverseCallback):void
        {
            super.project(parent, camera, callback);
    
            var view:Matrix3D = this.view;
    
            // Camera
            var m11:Number = view.n11;
            var m12:Number = view.n12;
            var m13:Number = view.n13;
            var m21:Number = view.n21;
            var m22:Number = view.n22;
            var m23:Number = view.n23;
            var m31:Number = view.n31;
            var m32:Number = view.n32;
            var m33:Number = view.n33;
    
            var vertices:Array = this.vertices;
            var i:int = vertices.length;
    
            var focus:Number = camera.focus;
            var zoom:Number = camera.zoom;
            var vertex:Vertex3D;
            var screen:Vertex2D;
            var persp:Number;
    
            while (vertex = vertices[--i])
            {
                // Center position
                var vx:Number = vertex.x;
                var vy:Number = vertex.y;
                var vz:Number = vertex.z;
    
                var sx:Number = vx * m11 + vy * m12 + vz * m13 + view.n14;
                var sy:Number = vx * m21 + vy * m22 + vz * m23 + view.n24;
                var sz:Number = vx * m31 + vy * m32 + vz * m33 + view.n34;
    
                if (vertex.projected == null)
                    vertex.projected = new Vertex2D();
                        
                screen = vertex.projected;
    			
              /*   if (screen.visible = (sz > 0) || screen.z == )
                { */
                    persp = focus / (focus + sz) * zoom;
    
                    screen.x = sx * persp;
                    screen.y = sy * persp;
                    screen.z = sz;
               /*  } else  {
                } */
                	//trace( sz );
            }
        }
        
        override public function dispose() : void {
            var i:int = vertices.length;
            
            
            while (--i > -1)
            {
            	//vertices[i] = null;
            	delete vertices[i];
            }
            vertices = null;
            
        	super.dispose();
        	
        }
    }
}
